﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.ORG.Entity;
using FastORM.Applicaiton;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("AttachConfig")]
    public class AttachConfigController : FastTemplateController<Frame_AttachConfig>
    {
        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/AttachConfig/AttachConfig_List.cshtml");
        }
        [Route("Add")]
        public ActionResult Add()
        {
            return View("~/Views/Frame/AttachConfig/AttachConfig_Add.cshtml");
        }
        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/AttachConfig/AttachConfig_Edit.cshtml");
        }


        [HttpPost, Route("FindList")]
        public override ActionResult FindList()
        {
            JObject Params = GetParams();
            string ConfigInfo = Convert.ToString(Params["ConfigInfo"]);
            if (!string.IsNullOrEmpty(ConfigInfo))
            {
                ListWhereText += string.Format(" and ConfigInfo='{0}'", ConfigInfo);
            }
            return base.FindList();
        }

        [HttpPost, Route("FindDetail")]
        public override ActionResult FindDetail()
        {
            return base.FindDetail();
        }

        [HttpPost, Route("OperateAdd")]
        public override ActionResult OperateAdd(Frame_AttachConfig Entity)
        {
            ReturnValue result = new ReturnValue();
            DBRow row = new DBRow("Frame_AttachConfig", "ConfigName", Entity.ConfigName);
            if (row.IsExist())
            {
                result.Add("code", -100);
                result.Add("msg", "已存在相同值的附件库！");
                return result;
            }
            else
            {
                return base.OperateAdd(Entity);
            }

        }

        [HttpPost, Route("OperateUpdate")]
        public override ActionResult OperateUpdate()
        {
            return base.OperateUpdate();
        }

        [HttpPost, Route("OperateBatchDelete")]
        public override ActionResult OperateBatchDelete()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            bool IsHave = false;
            string keyguidlist = Convert.ToString(Params["KeyGuidList"]);
            foreach (string KeyGuid in keyguidlist.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList())
            {
                Frame_AttachConfig AttachConfig = DB_Fast.FindOne(KeyGuid);
                string sql = string.Format("select count(1) from Frame_AttachInfo where ConfigName='{0}'", AttachConfig.ConfigName);
                if (Convert.ToInt32(@DBUtil.ExecuteScar(sql)) > 0)
                {
                    IsHave = true;
                    break;
                }
            }
            if (!IsHave)
            {
                DB_Fast.BatchDelete(keyguidlist.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
            }
            else
            {
                result.Add("code", -100);
                result.Add("msg", "存在附件使用附件库连接，无法删除");
            }
            return result;
        }
    }
}
